package com.eagle.support.helper;

import android.os.Handler;
import android.os.Message;

import com.eagle.support.common.AccountStatusManager;
import com.eagle.support.common.EventHandler;
import com.kinstalk.voip.sdk.api.WeaverService;

/**
 * Created by gc on 2017/1/9.
 */

public class AccountHelper implements EventHandler {

    private static AccountHelper sINSTANCE = null;

    protected IUserStateListener mUserStateListener;

    private AccountHelper() {
        AccountStatusManager.getInstance().setEventHandler(this);
        WeaverService.getInstance().registerUserStatusListener(AccountStatusManager.getInstance()); // 注册用户状态监听
    }

    public static AccountHelper getInstance() {
        if (sINSTANCE == null) {
            sINSTANCE = new AccountHelper();
        }
        return sINSTANCE;
    }

    @Override
    public Handler getHandler() {
        return mHandler;
    }

    public void registerUserStateListener(IUserStateListener listener) {
        this.mUserStateListener = listener;
    }

    public void unRegisterUserStateListener() {
        this.mUserStateListener = null;
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case KEY_LOGIN_RESULT:
                    if (mUserStateListener != null) mUserStateListener.onUserStateHandler(msg.arg1);
                    break;
            }
        }
    };

    public void login(String uid, String password, String appKey, String appSecret, String appSource) {
        AccountStatusManager.getInstance().login(uid, password, appKey, appSecret, appSource);
    }

    public void logout() {
        AccountStatusManager.getInstance().logout();
    }

    public boolean isLogged(){
        return AccountStatusManager.getInstance().isLogged();
    }

    public void onDestory() {
        AccountStatusManager.getInstance().setEventHandler(null);
        WeaverService.getInstance().unregisterUserStatusListener(AccountStatusManager.getInstance()); // 注册用户状态监听
    }

    public interface IUserStateListener {
        void onUserStateHandler(int state);
    }
}
